package net.cn_su.sharpeyes.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import net.cn_su.sharpeyes.R;
import net.cn_su.sharpeyes.baseadapter.AdapterCallback;
import net.cn_su.sharpeyes.beans.DangerInfo;
import net.cn_su.sharpeyes.beans.RatedInfo;
import net.cn_su.sharpeyes.utils.DateTool;
import net.cn_su.sharpeyes.view.leanback.mode.OpenPresenter;

import java.util.List;

/**
 * 测试.
 * Created by hailongqiu on 2016/8/24.
 */
public class TvDangerPresenter extends OpenPresenter {

    private  List<DangerInfo> mList;

    private AdapterCallback mCallback;

    private Context mContext;
    /**
     * 用来添加足部
     */
    private DangerInfo temp;
    public TvDangerPresenter(List<DangerInfo> mList, AdapterCallback mCallback, Context context) {
        this.mList = mList;
        if (mList.size()>0){
            temp=new DangerInfo();
            mList.add(temp);
        }
        mContext=context;
        this.mCallback=mCallback;

    }

    @Override
    public int getItemCount() {
        return mList.size();
    }


    @Override
    public int getItemViewType(int position) {

        if (position == mList.size()-1) {
            return 1;
        }


        return 0;
    }

    @Override
    public void addAll(List<DangerInfo> mList) {
        if (mList != null && mList.size() > 0) {

            if (temp!=null){
                this.mList.remove(temp);
            }

            this.mList.addAll(mList);
            if (temp!=null){
                this.mList.add(temp);
            }

        }
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == 0) {
            View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_call_back, parent, false);
            return new DangerViewHolder(view);
        } else {
            return new AddMoreViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_add_more, parent, false));
        }
    }


    @Override
    public void onBindViewHolder(ViewHolder viewHolder, int position) {
        if (getItemViewType(position) == 0) {
            final DangerInfo info = mList.get(position);
            DangerViewHolder gridViewHolder = (DangerViewHolder) viewHolder;
            ImageView iv_type = gridViewHolder.iv_type;
            TextView tv_time01 = gridViewHolder.tv_time01;
            TextView tv_camera = gridViewHolder.tv_camera;
            TextView tv_address = gridViewHolder.tv_address;
            TextView tv_type = gridViewHolder.tv_type;
            TextView tv_state= gridViewHolder.tv_state;
            TextView btnPinJia= gridViewHolder.tv_pinjia;
            LinearLayout ll_data=gridViewHolder.ll_data;
            tv_time01.setText(DateTool.timesToStrTime(info.getCall_date_timestamp() + "", "yyyy-MM-dd HH:mm"));
            tv_camera.setText(info.getCamera());
            tv_address.setText(info.getCamera_adress());
            tv_type.setText(info.getCall_type());
            tv_state.setText(info.getStatus());


            if (info.getStatus_code() == 4 || info.getStatus_code() == 5) {


                if (info.getStatus_code() == 4){
                    //显示处理时间
                    try {
                        tv_state.setText(DateTool.timesToStrTime(info.getCompleted_at() + "", "yyyy-MM-dd HH:mm"));
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }

                btnPinJia.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_confirm));

                //判断是否有评价内容
                List<RatedInfo> ratedInfos = info.getRated_info();
                ll_data.setEnabled(true);
                if (ratedInfos.size() == 0) {

                    btnPinJia.setText("评价");
                    ll_data.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mCallback.adapterInfotoActiity(info,2);
                        }
                    });
                } else {

                    btnPinJia.setText("查看评价");
                    ll_data.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            mCallback.adapterInfotoActiity(info,3);
                        }
                    });
                }

            } else {
                btnPinJia.setText("评价");
                btnPinJia.setBackgroundDrawable(mContext.getResources().getDrawable(R.drawable.btn_gray02));
                ll_data.setEnabled(false);
            }





            switch (info.getCall_type_id()) {
                case 1051:
                    iv_type.setImageResource(R.drawable.icon_call_fire);
                    break;
                case 1052:
                    iv_type.setImageResource(R.drawable.icon_call_hurt);
                    break;
                case 1053:
                    iv_type.setImageResource(R.drawable.icon_call_steal);
                    break;
                default:
                    iv_type.setImageResource(R.drawable.icon_call_other);
                    break;
            }
        } else {


            AddMoreViewHolder addMoreViewHolder = (AddMoreViewHolder) viewHolder;

            addMoreViewHolder.ll_add.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mCallback.adapterInfotoActiity(null,1);
                }
            });


        }


    }

}
